﻿using System.Data;
using System.Text;
using System.Web;
using System.Xml;
using DAL;
using NLog;

/// <summary>
/// RSS writer that emits RSS feeds. 
/// </summary>
public class RSSFeedsGeneraters
{

    #region Variable
    private static Logger _logger = LogManager.GetCurrentClassLogger();
    #endregion

    /// <summary>
    /// return the last 250 rows and put in RSS File "LiveKoraRSS"
    /// </summary>
    public void DoRSSGenerater()
    {
        try
        {
            ManageArticls objManageArticls = new ManageArticls();
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "text/xml";
            XmlTextWriter xtwFeed = new XmlTextWriter(HttpContext.Current.Server.MapPath("~/Settings_App/LiveKoraRSS.xml"), Encoding.UTF8);
            xtwFeed.WriteStartDocument();
            xtwFeed.WriteStartElement("rss");
            xtwFeed.WriteAttributeString("version", "2.0");
            xtwFeed.WriteStartElement("channel");
            xtwFeed.WriteElementString("title", "LiveKora.net Sport (كرة قدم)");
            xtwFeed.WriteElementString("link", "http://livekora.net");
            xtwFeed.WriteElementString("description", "LiveKora.net for sport news ليف كورة لاخبار الرياضة المحلية و العالمية");
            xtwFeed.WriteElementString("copyright", "© 2013 LiveKora.net");

            foreach (DataRow row in objManageArticls.GetAllArticlsForRSSFeeds().Tables[0].Rows)
            {
                xtwFeed.WriteStartElement("item");
                xtwFeed.WriteElementString("title", row["Articls_Title"].ToString());
                xtwFeed.WriteElementString("description", row["Articls_SmallDescription"].ToString());
                xtwFeed.WriteElementString("link", string.Format("http://livekora.net/Articles/{0}/", row["Articls_ID"]));
                //xtwFeed.WriteElementString("category", row["Category_ID"].ToString());
                //xtwFeed.WriteElementString("contant", row["Articls_FullDescription"].ToString());

                if (row["Articls_Vedio"].ToString() != "null")
                {
                    xtwFeed.WriteElementString("Vedio", row["Articls_Vedio"].ToString());
                }

                if (row["Articls_Image"].ToString() != "null")
                {
                    string str = row["Articls_Image"].ToString();//to remove the first 2 chracter ..
                    str = str.Remove(0, 3);//to remove the first 2 chracter ..
                    xtwFeed.WriteElementString("Image", string.Format("http://livekora.net/") + str);
                    str = string.Empty;
                }
                xtwFeed.WriteElementString("pubDate", row["Articls_CreateDate"].ToString());
                xtwFeed.WriteEndElement();
            }

            xtwFeed.WriteEndElement();
            xtwFeed.WriteEndElement();
            xtwFeed.WriteEndDocument();
            xtwFeed.Flush();
            xtwFeed.Close();
        }
        catch (System.Exception ex)
        {
            _logger.Error("RSSFeedsGeneraters:::DoRSSGenerater():::" + ex.Message);
        }

    }

    /// <summary>
    /// return the last 250 rows and put in RSS File "LiveKoraRSS_Android"
    /// </summary>
    public void DoRSSGeneraterForAndroidApps()
    {
        try
        {
            ManageArticls objManageArticls = new ManageArticls();
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType = "text/xml";
            XmlTextWriter xtwFeed = new XmlTextWriter(HttpContext.Current.Server.MapPath("~/Settings_App/LiveKoraRSS_Android.xml"), Encoding.UTF8);
            xtwFeed.WriteStartDocument();
            xtwFeed.WriteStartElement("rss");
            xtwFeed.WriteAttributeString("version", "2.0");
            xtwFeed.WriteStartElement("channel");
            xtwFeed.WriteElementString("title", "LiveKora.net Sport (كرة قدم)");
            xtwFeed.WriteElementString("link", "http://livekora.net");
            xtwFeed.WriteElementString("description", "LiveKora.net for sport news for Mobile Apps ليف كورة لاخبار الرياضة المحلية و العالمية");
            xtwFeed.WriteElementString("copyright", "© 2013 LiveKora.net");

            foreach (DataRow row in objManageArticls.GetAllArticlsAndroidApps().Tables[0].Rows)
            {
                xtwFeed.WriteStartElement("item");
                xtwFeed.WriteElementString("title", row["Articls_Title"].ToString());
                xtwFeed.WriteElementString("description", row["Articls_SmallDescription"].ToString());
                xtwFeed.WriteElementString("link", string.Format("http://livekora.net/Articles/{0}/", row["Articls_ID"]));
                xtwFeed.WriteElementString("category", row["Category_ID"].ToString());
                xtwFeed.WriteElementString("contant", row["Articls_FullDescription"].ToString());

                if (row["Articls_Vedio"].ToString() != "null")
                {
                    xtwFeed.WriteElementString("Vedio", row["Articls_Vedio"].ToString());
                }

                if (row["Articls_Image"].ToString() != "null")
                {
                    string str = row["Articls_Image"].ToString();//to remove the first 2 chracter ..
                    str = str.Remove(0, 3);//to remove the first 2 chracter ..
                    xtwFeed.WriteElementString("Image", string.Format("http://livekora.net/") + str);
                    str = string.Empty;
                }
                xtwFeed.WriteElementString("pubDate", row["Articls_CreateDate"].ToString());
                xtwFeed.WriteEndElement();
            }

            xtwFeed.WriteEndElement();
            xtwFeed.WriteEndElement();
            xtwFeed.WriteEndDocument();
            xtwFeed.Flush();
            xtwFeed.Close();
        }
        catch (System.Exception ex)
        {
            _logger.Error("RSSFeedsGeneraters:::DoRSSGeneraterForAndroidApps():::" + ex.Message);
        }

    }
}